<?php
$indexStructures = array(U1_Building::INDEX, U1_Research::INDEX, U1_Army::INDEX);

//sélection de la structure
if (isset($_POST['structure']) && in_array($_POST['structure'], $indexStructures, true))
{
	$page['vars']['indexStructures'] = array($_POST['structure']);
	$page['requestKeep']['structure'] = $_POST['structure'];

	//construction d'une structure
	if (isset($_POST['action'], $_POST['planet'], $_POST['id']))
	{
		$id_p = (int)$_POST['planet'];
		$action = array();

		try{
			$planets->setCurrentId($id_p);
			//id de la liste ou de l'élément
			$id = (int)$_POST['id'];
			$structure = $planets->getInstance($_POST['structure'], $id_p);

			if ($_POST['action'] === 'build' && $structure->exists($id)
			 || $_POST['action'] === 'down' && $structure->existsList($id))
			{
				$level = (isset($_POST['level']) && (int)$_POST['level'] > 1)
					? (int)$_POST['level']
					: 1;

				$action = array(
					'id' => ($_POST['action'] === 'down')
						? $structure->getList($id)->getIdElement()
						: $id,
					'action' => $_POST['action']
				);

				try{
					$action['level'] = $structure
						->$action['action']($id, $_SERVER['REQUEST_TIME'], $level)
						->getLevel();
				}
				catch (SW_Exception $e)
				{
					$action['error'] = $e->getMessage();
					if ($action['action'] === 'build')
						$action['level'] = ($structure instanceof U1_Army)
							? $level
							: $level + $structure->get($id)->getLevel();
				}

				$page['vars']['actions'][$id_p][$_POST['structure']][] = $action;
			}
		}
		catch(SW_Planet_Exception $e)
		{}
	}
}
else if (isset($_GET['structure']) && in_array($_GET['structure'], $indexStructures, true))
{
	$page['vars']['indexStructures'] = array($_GET['structure']);
	$page['requestKeep']['structure'] = $_GET['structure'];
}
else
	$page['vars']['indexStructures'] = $indexStructures;

$page['vars']['allIndexStructures'] = $indexStructures;